﻿package
{
	import flash.display.Stage;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.utils.setInterval;

	import Global;
	
	import effects.*;
	import classes.*;
	/*
	Class: BackMixer
	A class that creates and manages BackMixer Object.
	*/
	public class BackMixer extends Sprite
	{
		/*
         Variable: fxs
         public Array, holds available effects.
		*/
		public var fxs:Array;
		
		/*
         Variable: af
         public Effect, current Effect.
		*/
		public var af:Effect;
		
		/*
         Variable: dur
         public uint, holds and sets duration for transition between effects in fxs. default is 60.
		*/
		public var dur:uint = 60;
		
		/*
         Variable: cf
         private int, used internaly.
		*/
		private var cf:int;
		
		/*
         Constructor: BackMixer
         Initializes the object.
		 
		 Parameters: _stage
		 BackMixer needs a stage:Stage.
		*/
		public function BackMixer(_stage:Stage)
		{
			_stage.align="LT";
			_stage.scaleMode="noScale";
			_stage.addChild(this);
			Global._stage=_stage;
			
			fxs=[
			new Effect0(),
			new Effect1(),
			new Effect2(),
			new Effect3(),
			new Effect4(),
			new Effect5(),
			new Effect6()
			];
			
			cf = 0;
			af=fxs[0];
			addChild(af);
			setInterval(changeEffect,dur*1000);
		}
		
		/*
         Function: getEffect
         returns Effect, gets next Effect to play.
		*/
		public function getEffect():Effect
		{
			cf=(cf==fxs.length-1)?0:cf+1;
			return fxs[cf];
		}
		
		/*
         Function: changeEffect
         changes af in order from available Effects in fxs.
		*/		
		public function changeEffect():void
		{
			var tmpaf:Effect = getEffect();
			tmpaf.alpha=0;
			tmpaf.add();
			addChild(tmpaf);
			
			TweenLite.to(af, 1, {alpha:0,onComplete:remove});
			TweenLite.to(tmpaf, 2, {alpha:1,onComplete:pot});
				
			function remove():void
			{
				af.remove();
				removeChild(af);
			}
			function pot():void
			{
				af=tmpaf;
			}
		}
	}
}